/**
 * Created by Arnald on 2017/6/10.
 */
let md5 = require('md5');
let userDao = require('../mysql/userDao.js');
let regDao = require('../mysql/regDao.js');
let advanceDao = require('../mysql/advanceDao.js');
let gameData = require('../util/gameData.js');

let create = module.exports;

create.auto = function (cb) {
    advanceDao.autoAccount(function (err, res) {
        let account = 'auto' + res;
        let password = parseInt(Math.random() * 89999999 + 10000000).toString();
        create.user(account, password, function (err, res, token) {
            cb(err, res, {'account': account, 'password': password, 'token': token});
        });
    });
};
create.user = function (account, password, cb) {
    let token = create.token(); 
    let userModel = new userDao();
    userModel.set('account', account);
    userModel.set('name', account);
    userModel.set('password', md5(password));
    userModel.set('gold', gameData.d.config.regGold);
    userModel.set('card', gameData.d.config.regCard);
    userModel.set('token', token);
    userModel.save(function (err, res) {
        if (res == null){
            cb(err, res, token);
        }
        let date = new Date().Format("yyyy-MM-dd hh:mm:ss");
        let regModel = new regDao();
        regModel.set('userId', res.res.insertId);
        regModel.set('date', date);
        regModel.save(function () {
        });
        cb(err, res, token);
    });
};

create.blued = function (uid, name, avatar, cb) {
    let token = create.token();
    let userModel = new userDao();
    userModel.set('bluedId', uid);
    userModel.set('account', 'blued_' + uid);
    userModel.set('name', name);
    if (avatar && avatar != null) {
        userModel.set('avatar', avatar);
    }
    userModel.set('password', md5('blued_' + uid + '_password'));
    userModel.set('gold', gameData.d.config.regGold);
    userModel.set('card', gameData.d.config.regCard);
    userModel.set('token', token);
    userModel.save(function (err, res) {
        if (res == null){
            cb(err, res, token);
        }
        let date = new Date().Format("yyyy-MM-dd hh:mm:ss");
        let regModel = new regDao();
        regModel.set('userId', res.res.insertId);
        regModel.set('date', date);
        regModel.save(function () {
        });
        cb(err, res, token);
    });
};

create.lovelive = function (uid,groupid,name,avatar, cb) {
    let token = create.token(); 
    let userModel = new userDao(); 
    userModel.set('groupId', groupid);
    userModel.set('account', uid); 
    if (name && name != null) {
        userModel.set('name', name);
    }   
    if (avatar && avatar != null) {
        userModel.set('avatar', avatar);
    }   
    userModel.set('password', md5(uid));
    userModel.set('gold', gameData.d.config.regGold);
    userModel.set('card', gameData.d.config.regCard);
    userModel.set('token', token);
    userModel.save(function (err, res) {
        if (res == null) {
            cb(err, res, token);
        }   
        let date = new Date().Format("yyyy-MM-dd hh:mm:ss");
        let regModel = new regDao();
        regModel.set('userId', res.res.insertId);
        regModel.set('date', date);
        regModel.save(function () {
        });
        cb(err, res, token);
    });
};

create.weixin = function (uid, name, avatar, cb) {
    let token = create.token();
    let userModel = new userDao(); 
    userModel.set('account', uid);
    userModel.set('weixinId', uid);
    userModel.set('name', name);
    if (avatar && avatar != null) {
        userModel.set('avatar', avatar);
    }
    userModel.set('password', md5('123456'));
    userModel.set('gold', gameData.d.config.regGold);
    userModel.set('card', gameData.d.config.regCard);
    userModel.set('token', token);
    userModel.save(function (err, res) {
        if (err!=null || res == null){
            cb(err, null);
            return;
        }   
        let date = new Date().Format("yyyy-MM-dd hh:mm:ss");
        let regModel = new regDao();
        regModel.set('userId', res.res.insertId);
        regModel.set('date', date);
        regModel.save(function () {
        }); 
        cb(err, res, token);
    }); 
};  

create.reToken = function (userModel, param, cb) {
    let random = parseInt(100000 + (999999 - 100000) * (Math.random()));
    let token = md5(userModel.data.id + '&' + random);
    userModel.set('token', token);
    if (param && param.name && param.name != userModel.data.name) {
        userModel.set('name', param.name);
    }
    if (param && param.avatar && param.avatar != null && param.avatar != userModel.data.avatar) {
        userModel.set('avatar', param.avatar);
    }
    userModel.save(function (err, res) {
        if (err != null) {
            cb(err, null);
            return false;
        }   
        cb(null, {'token': token});
        return false;
    });
};


create.token = function () {
    let random = parseInt(100000 + (999999 - 100000) * (Math.random()));
    let token = md5(Date.now() + '&' + random);
    return token;
};
